package kr.couponplay.android.activity.search;

import java.util.List;

import kr.couponplay.android.activity.R;
import kr.couponplay.android.domain.deal.Deal;
import kr.couponplay.android.domain.deal.DealList;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.support.v4.app.FragmentTransaction;

public class SearchFragmentPagerAdapter extends FragmentStatePagerAdapter {
	private SearchActivity activity;
	private List<Deal> deals;
	private String query;
	
	public SearchFragmentPagerAdapter(SearchActivity activity, DealList dealList) {
		super(activity.getSupportFragmentManager());
        this.activity = activity;
        this.deals = dealList.getDeals();
        this.query = dealList.getQuery();
    }
	
	public void replace() {
		for (int i = 0; i < getCount(); i++) {
			FragmentTransaction transaction = activity.getSupportFragmentManager().beginTransaction();
			transaction.replace(R.id.search_viewpager, getItem(i));
			transaction.commit();
		}
        notifyDataSetChanged();
    }	

	@Override
	public Fragment getItem(int position) {
		if (position == 1) {
			return SearchResultFragment.newInstance(activity, deals, query);
		} else {
			return RecentQueryFragment.newInstance(activity);
		}
    }
	
	@Override
	public int getItemPosition(Object item) {
		PositionAware positionAware = (PositionAware)item;
		return positionAware.getPosition();
    }
	
	@Override
	public int getCount() {
		return SearchSubMenuHelper.getSubMenuButtons().size();
	}
}
